package org.kgduyjj.toolbox.bean;

import java.util.ArrayList;
import java.util.List;

import org.kgduyjj.toolbox.StringUtils;
import org.kgduyjj.toolbox.exception.SimpleToolboxException;

/**
 * 简单的REDIS KEY 构建方式
 * 
 * @author Kgduyjj
 * @since 2020-07-30
 * @version Alpha-1.43
 *
 */
public class SimpleRedisKey {
	private static final String MOSAICS = ":";
	private static final int MIN_KEY_INDEX = 2;
	// 表名
	protected final String tableName;
	// 识别键位（作为主键识别的键位）
	protected final String serialKey;
	// 识别键位值
	protected final String serialValue;
	// 非必要参数：补充识别位
	protected final List<String> supplyColumns;

	private SimpleRedisKey(Builder builder) {
		this.tableName = builder.tableName;
		this.serialKey = builder.serialKey;
		this.serialValue = builder.serialValue;
		this.supplyColumns = builder.supplyColumns;
	}

	public String generate() {
		StringBuffer buffer = new StringBuffer();
		if (StringUtils.isNotBlank(tableName)) {
			buffer.append(tableName);
		}
		_paraphrase(buffer, serialKey);
		_paraphrase(buffer, serialValue);
		if (supplyColumns != null) {
			for (String supplyColumn : supplyColumns) {
				_paraphrase(buffer, supplyColumn);
			}
		}
		return buffer.toString();
	}

	private StringBuffer _paraphrase(StringBuffer buffer, String other) {
		if (StringUtils.isNotBlank(other))
			return buffer.append(MOSAICS).append(other);
		return buffer;
	}

	public static SimpleRedisKey rebuild(String keyString) {
		if (StringUtils.isBlank(keyString)) {
			return null;
		}
		String[] keyArray = keyString.split(MOSAICS);
		if (keyArray == null || keyArray.length < MIN_KEY_INDEX) {
			throw new SimpleToolboxException("RedisKey Is Illegal;");
		}
		Builder builder = new SimpleRedisKey.Builder(keyArray[0], keyArray[1], keyArray[2]);
		if (keyArray.length > MIN_KEY_INDEX) {
			for (int i = MIN_KEY_INDEX + 1; i < keyArray.length; i++) {
				builder.supplyColumn(keyArray[i]);
			}
		}
		return builder.build();
	}

	public static class Builder {
		// 表名
		protected String tableName;
		// 识别键位（作为主键识别的键位）
		protected String serialKey;
		// 识别键位值
		protected String serialValue;
		// 非必要参数：补充识别位
		protected List<String> supplyColumns;

		public Builder(String tableName, String serialKey, String serialValue) {
			super();
			this.tableName = tableName;
			this.serialKey = serialKey;
			this.serialValue = serialValue;
		}

		public Builder supplyColumn(String column) {
			if (supplyColumns == null && StringUtils.isNotBlank(column)) {
				supplyColumns = new ArrayList<String>();
			}
			supplyColumns.add(column);
			return this;
		}

		public Builder supplyColumns(String... columns) {
			if (supplyColumns == null && StringUtils.isNotBlank(columns[0])) {
				supplyColumns = new ArrayList<String>();
			}
			for (String column : columns) {
				if (StringUtils.isNotBlank(column)) {
					supplyColumns.add(column);
				}
			}
			return this;
		}

		public SimpleRedisKey build() {
			SimpleRedisKey simpleRedisKey = new SimpleRedisKey(this);
			return simpleRedisKey;
		}
	}
}
